package com.rpay.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import com.rpay.common.util.cyptor.Cryptor;
import com.rpay.dao.mapper.RpayAdminUserMapper;
import com.rpay.model.admin.RpayAdminModule;
import com.rpay.model.admin.RpayAdminUser;
import com.rpay.service.admin.ISystemUserService;
import com.rpay.service.base.impl.BaseServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.RowBoundsMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 */
@Service(value = "systemUserServiceImpl")
public class SystemUserServiceImpl extends BaseServiceImpl<RpayAdminUser> implements ISystemUserService {
    Logger logger = LoggerFactory.getLogger(SystemUserServiceImpl.class);

    @Autowired
    private RpayAdminUserMapper sysUserMapper;

    @Autowired
    private ModuleServiceImpl moduleService;
    @Autowired
    @Qualifier("readSqlSession")
    private SqlSessionTemplate readSqlSession;

    public List<RpayAdminUser> selectAll() {
        return sysUserMapper.selectAll();
    }

    /**
     * 当用户角色变更或删除时，需要更新缓存中的信息
     *
     * @param name
     * @return
     */
    @Override
    //@Cacheable(value = "user", condition = "#name != null")
    public RpayAdminUser findByName(String name) {
        RpayAdminUser systemUser = new RpayAdminUser();
        systemUser.setUsername(name);
        return sysUserMapper.selectOne(systemUser);
    }

    public RpayAdminUser findByUser(RpayAdminUser user) {
        user.setPassword(Cryptor.crypto(user.getPassword()));
        return sysUserMapper.selectOne(user);

    }

    public RpayAdminUser findByUsernoPasswd(RpayAdminUser user) {
        return sysUserMapper.selectOne(user);
    }

    @Override
    public List<RpayAdminUser> getUserListByUserAndGeo(Map user) {
        return getSqlSessionTemplate().selectList("getUserListByUserAndGeo", user);
    }

    @Override
    public Long getTotalByUser(Map user) {
        return getSqlSessionTemplate().selectOne("getUserCountByUserAndGeo", user);
    }


    public List<Map> getUserList(Map sysuser, RowBounds rowBounds) {
        List list = readSqlSession.selectList("com.rpay.dao.mapper.RpayAdminUserMapper.getUserListByUserNew",
                sysuser, rowBounds);
        return list;
    }

    public int getUserCount(Map param) {
        return readSqlSession.selectOne("com.rpay.dao.mapper.RpayAdminUserMapper.getUserCount", param);
    }

    public List<RpayAdminUser> getUserListByOrgId(Long orgId) {
        return getSqlSessionTemplate().selectList("getUserListByOrgid", orgId);
    }

    public List<Long> findSonOrgList(Long orgId) {
        return sysUserMapper.findSonOrgList(orgId);
    }


    public RpayAdminUser findUserByOrgId(RpayAdminUser user) {
        return getSqlSessionTemplate().selectOne("getUserByOrgId", user.getOrgId());
    }

    @Override
    public RpayAdminUser findUserByOpenid(String openid) {
        return getSqlSessionTemplate().selectOne("findUserByOpenid", openid);
    }


    public List getSonOrgIdList(Long orgId, boolean addself) {
        List<Long> orgList = new ArrayList<>();
        getSonOrgId(orgId, orgList);
        if (addself) {
            orgList.add(orgId);
        }
        return orgList;
    }

    private void getSonOrgId(Long orgId, List<Long> orgList) {
        List<Long> sonOrgIdList = findSonOrgList(orgId);
        if (sonOrgIdList != null && sonOrgIdList.size() > 0) {
            for (Long sonorgId : sonOrgIdList) {
                if (!orgList.contains(sonorgId)) {
                    orgList.add(sonorgId);
                    getSonOrgId(sonorgId, orgList);
                }
            }
        }
    }


    //根据shopId获取用户
    public RpayAdminUser getUserByShopId(Long shopId) {
        return getSqlSessionTemplate().selectOne("getUserByShopId", shopId);
    }

    public RpayAdminUser getParentManagerUser(RpayAdminUser user) {
        RpayAdminUser userManager = new RpayAdminUser();
        userManager.setOrgId(user.getParentOrgId());
        return findUserByOrgId(userManager);
    }

    public List<RpayAdminModule> findModuleList(Long userId) {
        return moduleService.listModuleByUserId(userId);
    }



}